<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="借款额度">
        <el-input-number v-model="integralGrade.borrowAmount" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间开始">
        <el-input-number v-model="integralGrade.integralStart" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间结束">
        <el-input-number v-model="integralGrade.integralEnd" :min="0" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate()"
        >
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { defineComponent } from "@vue/composition-api";
import integralGradeApi from "@/api/core/integral-grade";

export default {
  created() {
    //当路由中存在id属性的时候，就是回显表单，需要调用回显数据的接口
    if (this.$route.params.id) {
      this.fenfetchById(this.$route.params.id);
    }
  },

  data() {
    return {
      saveBtnDisabled: false,
      integralGrade: {} //积分等级对象
    };
  },

  methods: {
    //保存或者更细方法
    saveOrUpdate() {
      //禁用保存按钮
      this.saveBtnDisabled = true;

      //因为都是点击同一个保存按钮，需要根据积分对象id判断是新增还是修改
      if (!this.integralGrade.id) {
        //没有id，就是新增积分对象
        this.save();
      } else {
        //有id，就是修改
        this.update();
      }
    },

    save() {
      integralGradeApi.save(this.integralGrade).then(response => {
        this.$message({
          type: "success",
          message: response.message
        });
        this.$router.push("/core/integral-grade/list");
      });
    },

    fenfetchById(id) {
      integralGradeApi.fenfetchById(id).then(response => {
        this.integralGrade = response.data.record;
      });
    },

    update() {
      integralGradeApi.update(this.integralGrade).then(response => {
        this.$message.success(response.message);
        this.$router.push("/core/integral-grade/list");
      });
    }
  }
};
</script>
